package net.ptcs.litang.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import net.ptcs.litang.dao.TicketDao;
import net.ptcs.litang.entity.Users;
import net.sf.json.JSONObject;

@WebServlet("/orderTicket")
public class OrderTicket extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置头文件
        response.setContentType("application/json; charset=utf8");
        HttpSession session = request.getSession();
        Users user = (Users) session.getAttribute("user");
        PrintWriter out = response.getWriter();
        String result = null;
        // 设置响应map
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        // 获取表单内容
        Integer id = Integer.parseInt(request.getParameter("id"));
        TicketDao ticketDao = new TicketDao();
        try {
            result = ticketDao.orderTicket(id, user.getId());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if (result != null && result.equals("1")) {
            hashMap.put("code", 0);
            hashMap.put("msg", "订票成功");
        } else {
            hashMap.put("code", 1);
            hashMap.put("msg", result);
        }
        JSONObject json = JSONObject.fromObject(hashMap);
        out.write(json.toString());
    }
}
